package com.ybfenpan.controller.admin.sys;


import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Member;
import com.ybfenpan.model.WageCurrent;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.WageCurrentService;
import com.ybfenpan.utils.CryptUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2016/3/25.
 */
@Controller

public class PersonInController extends BaseController {
	@Resource
	private MemberService memberService;
	@Resource
	private WageCurrentService wageCurrentService;
	
    @RequestMapping("admin/sys/personIn.shtml")
    public String add(@RequestParam MultipartFile file, ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	 
     
		HSSFWorkbook wb=new HSSFWorkbook(file.getInputStream());			 //获得工作薄
		HSSFSheet sheet=wb.getSheetAt(0);

		Integer num1=sheet.getLastRowNum();				//最后行号(个数等于行号+1)		
		
		for(int i=0;i<num1;i++){
			HSSFRow row=sheet.getRow(i+1);				 		//0行是标题
			
			//看学号是否为空，如果为空就break
			if(row==null){
				break;
			}
			Member member = new Member(); 
			System.out.println(row.getCell((short)0).toString());
			double idDouble = Double.parseDouble(row.getCell((short)0).toString());
			int id = (int)idDouble;
			member.setId(id);
			
			String loginName =  row.getCell((short)1).toString();		
			member.setLoginName(loginName);
			
			String onePsw= row.getCell((short)2).toString();
			String onePassword = CryptUtil.getInstance().encrypt2String(onePsw,CryptUtil.keyStr);
			member.setOnePassword(onePassword);
			
			String twoPsw= row.getCell((short)3).toString();
			String twoPassword = CryptUtil.getInstance().encrypt2String(twoPsw,CryptUtil.keyStr);
			member.setTwoPassword(twoPassword);
			
			String accountUse  = null;		
			if(row.getCell((short)4).toString().equals("TRUE")){
				accountUse="T";
			}else {
				accountUse="F";
			}
			member.setAccountUse(accountUse);
			
			String accountXia   = null;		
			if(row.getCell((short)5).toString().equals("TRUE")){
				accountXia="T";
			}else {
				accountXia="F";
			}
		 
			String stateZiliao    = null;
			System.out.println(row.getCell((short)6));
			if(row.getCell((short)6).toString().equals("TRUE")){
				System.out.println("T");
				stateZiliao="T";
			}else {
				System.out.println("F");
				stateZiliao="F";
			}
			member.setStateZiliao(stateZiliao);
			
			String stateWage = null;		
			if(row.getCell((short)7).toString().equals("TRUE")){
				stateWage ="T";
			}else {
				stateWage ="F";
			}
			member.setStateWage(stateWage);
			
			double dailiIdDouble =Double.parseDouble(row.getCell((short)8).toString());
			int dailiId=(int)dailiIdDouble;
			member.setDailiId(dailiId);
			
			Double money = Double.parseDouble(row.getCell((short)9).toString());
			member.setMoney(money);
			
			Double ele = Double.parseDouble(row.getCell((short)10).toString());
			member.setEle(ele);
			
			String activeDateString  =row.getCell((short)11).toString();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");                
			Date activeDate = sdf.parse(activeDateString);       
			member.setActiveDate(activeDate);
			
			String registerDateString  =row.getCell((short)12).toString();
			Date registerDate = sdf.parse(registerDateString);
			member.setRegisterDate(registerDate);
			
			double recommendNumDouble  =Double.parseDouble(row.getCell((short)13).toString());
			int recommendNum = (int)recommendNumDouble ;
			member.setRecommendNum(recommendNum);
			
			Double recommendYe  = Double.parseDouble(row.getCell((short)14).toString());
			member.setRecommendYe(recommendYe);
			
			double recommendIdDouble;
			if(row.getCell((short)15)!=null&&!row.getCell((short)15).equals("")){
				recommendIdDouble =Double.parseDouble(row.getCell((short)15).toString());
				int recommendId = (int)recommendIdDouble;
				member.setRecommendId(recommendId);
			}
			
			double registerIdDouble ;
			if(row.getCell((short)16)!=null&&!row.getCell((short)16).equals("")){
				registerIdDouble =Double.parseDouble(row.getCell((short)16).toString());
				int registerId=(int)registerIdDouble;
				member.setRegisterId(registerId);
			}
			
			double activeIdDouble  ;
			if(row.getCell((short)17)!=null&&!row.getCell((short)17).equals("")){
				activeIdDouble =Double.parseDouble(row.getCell((short)17).toString());
				int activeId=(int)activeIdDouble;
				member.setActiveId(activeId);
			}
			
			String bankName  =  row.getCell((short)18).toString();
			member.setBankName(bankName);
			
			String bankAddress =  row.getCell((short)19).toString();
			member.setBankAddress(bankAddress);
			
			String bankSheng  =  row.getCell((short)20).toString();
			member.setBankSheng(bankSheng);
			
			String bankShi  =  row.getCell((short)21).toString();
			member.setBankShi(bankShi);
			
			String bankCardNo =  row.getCell((short)22).toString();
			member.setBankCardNo(bankCardNo);
			
			String bankPersonName  =  row.getCell((short)23).toString();
			member.setBankPersonName(bankPersonName);
			
			String personName   =  row.getCell((short)24).toString();
			member.setPersonName(personName);
			
			String tel   =  row.getCell((short)25).toString();
			member.setTel(tel);
			
			String qq   =  row.getCell((short)26).toString();
			member.setQq(qq);
			
			String wei   =  row.getCell((short)27).toString();
			member.setWei(wei);
			member.setAccountActive("T");
			this.memberService.saveOrUpdate(member);
			
			//新增工资表
			WageCurrent wageCurrent=new WageCurrent();	
			wageCurrent.setId(id);
			wageCurrent.setMemberId(id);		 //代理商编号
			wageCurrentService.saveOrUpdate(wageCurrent);
			
			 
			//更新对象信息
			Map<String, Object> mapMember = new HashMap<String, Object>();
			 
			mapMember.put("id", id);
			mapMember.put("wageCurrentId", id);
			 
			
			
			
			memberService.update(mapMember);
			
			System.out.println(loginName);
		}
    	
    	return "admin/sys/personIn";
		
     
    }
    @RequestMapping("admin/sys/personIn1.shtml")
    public String add1(@RequestParam MultipartFile file, ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	 
     
		HSSFWorkbook wb=new HSSFWorkbook(file.getInputStream());			 //获得工作薄
		HSSFSheet sheet=wb.getSheetAt(0);

		Integer num1=sheet.getLastRowNum();				//最后行号(个数等于行号+1)		
		
		for(int i=0;i<num1;i++){
			HSSFRow row=sheet.getRow(i+1);				 		//0行是标题
			
			//看学号是否为空，如果为空就break
			if(row==null){
				break;
			}
			
			 
			double idDouble = Double.parseDouble(row.getCell((short)0).toString());
			int id = (int)idDouble;
			 
			Double weiWage1 = Double.parseDouble(row.getCell((short)28).toString());
		 
			 
			//更新对象信息
			Map<String, Object> mapMember = new HashMap<String, Object>();
			 
			mapMember.put("id", id);
			mapMember.put("weiWage1", weiWage1);
		 
			memberService.update(mapMember);
			
		}
    	
    	return "admin/sys/personIn";
		
     
    }
    
    
}
